﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CommunitySystem.PageView.EmpBasicManager
{
    public partial class EmpDutyWindow : ChildWindow
    {
        private EmpbasicService.EmpBasicServiceClient Client = new EmpbasicService.EmpBasicServiceClient();
        private EmpbasicService.Emp_Duty model = new EmpbasicService.Emp_Duty();
        private EmpbasicService.Emp_Duty oldmodel = new EmpbasicService.Emp_Duty();
        private InsertSystemRemark insertsys = new InsertSystemRemark();
        private EmpbasicService.Emp_ResumeModel model1 = new EmpbasicService.Emp_ResumeModel();
        private EmpbasicService.OrgForHRModel orgmodel = new EmpbasicService.OrgForHRModel();
        public int ID;
        public int EmpID;
        public int OrgID;
        public bool IsNew = false;
        public EmpDutyWindow(int empID,int iD, bool isnew,int orgID)
        {
            InitializeComponent();
            ID = iD;
            EmpID = empID;
            IsNew = isnew;
            OrgID = orgID;
            rzzt.OnSelectionChanged += new OnSelectionChangedEvent(rzzt_OnSelectionChanged);
            zhiwushuxing.OnSelectionChanged += new OnSelectionChangedEvent(zhiwushuxing_OnSelectionChanged);
            Client.GetOrgForHRModelCompleted += new EventHandler<EmpbasicService.GetOrgForHRModelCompletedEventArgs>(Client_GetOrgForHRModelCompleted);
            if (IsNew)
            {
                this.Title = "新增职务信息";
                model = new EmpbasicService.Emp_Duty();
                this.DataContext = model;
            }
            else
            {
                this.Title = "编辑职务信息";
                Client.GetEmp_DutysCompleted += new EventHandler<EmpbasicService.GetEmp_DutysCompletedEventArgs>(Client_GetEmp_DutysCompleted);
                Client.GetEmp_DutysAsync("(" + EmpID + ")", ID);
            }
        }

        void zhiwushuxing_OnSelectionChanged(object sender, object NewValue, object OldValue)
        {
            if (NewValue.ToString() == "领导职务")
            {
                renqishiyongqi.Visibility = System.Windows.Visibility.Visible;
                renzhidaoqi.Visibility = System.Windows.Visibility.Visible;
                renqi.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                renqishiyongqi.Visibility = System.Windows.Visibility.Collapsed;
                renzhidaoqi.Visibility = System.Windows.Visibility.Collapsed;
                renqi.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        void rzzt_OnSelectionChanged(object sender, object NewValue, object OldValue)
        {
            if (NewValue.ToString() == "挂任")
            {
                gr.Visibility = System.Windows.Visibility.Visible;
                Client.GetOrgForHRModelAsync(OrgID);
            }
            else
            {
                gr.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        void Client_GetOrgForHRModelCompleted(object sender, EmpbasicService.GetOrgForHRModelCompletedEventArgs e)
        {
            if (e.Result != null && e.Result.Count > 0)
                orgmodel = e.Result[0];
        }

        void Client_GetEmp_DutysCompleted(object sender, EmpbasicService.GetEmp_DutysCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                model = e.Result[0];
                oldmodel = new EmpbasicService.Emp_Duty
                {
                    DutyNo = model.DutyNo,
                    DutyName = model.DutyName,
                    DutyLevel = model.DutyLevel,
                    DutyAttribute = model.DutyAttribute,
                    OKDate = model.OKDate,
                    OrganizationName = model.OrganizationName,

                    Sec = model.Sec,
                    WorkCharge = model.WorkCharge,
                    ServiceMode = model.ServiceMode,
                    IsExceptionalPromotion = model.IsExceptionalPromotion,
                    JobStatus = model.JobStatus,
                    JobDate = model.JobDate,
                    RenQi = model.RenQi,
                    RZSYQ = model.RZSYQ,
                    BRZDQRQ = model.BRZDQRQ,
                    LianRen = model.LianRen,
                    RXRZWSXSJ = model.RXRZWSXSJ,
                    GRStartDate = model.GRStartDate,
                    GREndDate = model.GREndDate
                };
            }
            this.DataContext = model;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (model.DutyAttribute == "非领导职务")
            {
                model.RenQi = 0;
                model.RZSYQ = 0;
                model.BRZDQRQ = null;
            }
            if (IsNew == true)
            {
                model.EmpBasicID = EmpID;
                Client.AddEmp_DutyCompleted += new EventHandler<EmpbasicService.AddEmp_DutyCompletedEventArgs>(Client_AddEmp_DutyCompleted);
                Client.AddEmp_DutyAsync(model);
            }
            else
            {
                if (!string.IsNullOrEmpty(begin.Text)) model.OKDate = Convert.ToDateTime(begin.Text);
                else
                    model.OKDate = null;
                if (!string.IsNullOrEmpty(end.Text)) model.JobDate = Convert.ToDateTime(end.Text);
                else
                    model.JobDate = null;
                if (!string.IsNullOrEmpty(renqi.Text)) model.BRZDQRQ = Convert.ToDateTime(renqi.Text);
                else
                    model.BRZDQRQ = null;
                Client.UpdateEmp_DutyCompleted += new EventHandler<EmpbasicService.UpdateEmp_DutyCompletedEventArgs>(Client_UpdateEmp_DutyCompleted);
                Client.UpdateEmp_DutyAsync(model);
            }
            if(model.JobStatus == "挂任")
            {
                model1.BeginDate = model.GRStartDate;
                model1.EndDate = model.GREndDate;
                model1.EmpBasicID = model.EmpBasicID;
                model1.Duty = model.DutyName;
                model1.Unit = orgmodel.Name;
                Client.AddEmp_ResumeCompleted += new EventHandler<EmpbasicService.AddEmp_ResumeCompletedEventArgs>(Client_AddEmp_ResumeCompleted);
                Client.AddEmp_ResumeAsync(model1);
            }
        }
        void Client_AddEmp_ResumeCompleted(object sender, EmpbasicService.AddEmp_ResumeCompletedEventArgs e)
        {
        }
        void Client_UpdateEmp_DutyCompleted(object sender, EmpbasicService.UpdateEmp_DutyCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("编辑成功！");
                InsertSystem("编辑");
                this.DialogResult = true;
            }
        }

        void Client_AddEmp_DutyCompleted(object sender, EmpbasicService.AddEmp_DutyCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("新增成功！");
                InsertSystem("新增");
                this.DialogResult = true;
            }
        }
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
        private string pattern = @"^[0-9]+([.]{1}[0-9]+){0,1}$";
        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            Match m = Regex.Match(((TextBox) sender).Text, pattern); // 匹配正则表达式
            if (!m.Success) // 输入的不是数字
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("只能输入数字！");
                ((TextBox) sender).Text = "";
                return;
            }
            if(((TextBox) sender).Name == "rq")
            {
                if(!string.IsNullOrEmpty(model.OKDate.ToString()) && model.OKDate != Convert.ToDateTime("1900-01-01"))
                {
                    renqi.Text = Convert.ToDateTime(model.OKDate).AddYears(Convert.ToInt32(((TextBox)sender).Text)).AddDays(-1).ToShortDateString();
                }
            }
        }

        void InsertSystem(string FangShi)
        {
            if (FangShi == "新增")
            {
                insertsys.Insert("职务子集", "", "", model.EmpBasicID, FangShi, "");
            }
            else
            {
                if (oldmodel.OKDate != model.OKDate)
                    insertsys.Insert("职务子集", oldmodel.OKDate == null ? null : Convert.ToDateTime(oldmodel.OKDate).ToShortDateString(),
                        model.OKDate == null ? null : Convert.ToDateTime(model.OKDate).ToShortDateString(),
                        model.EmpBasicID, FangShi, "批准时间");
                if (oldmodel.DutyNo != model.DutyNo)
                    insertsys.Insert("职务子集", oldmodel.DutyNo, model.DutyNo,
                        model.EmpBasicID, FangShi, "职务代码");
                if (oldmodel.DutyName != model.DutyName)
                    insertsys.Insert("职务子集", oldmodel.DutyName, model.DutyName,
                        model.EmpBasicID, FangShi, "职务名称");
                if (oldmodel.DutyLevel != model.DutyLevel)
                    insertsys.Insert("职务子集", oldmodel.DutyLevel, model.DutyLevel,
                        model.EmpBasicID, FangShi, "职务层次");
                if (oldmodel.DutyAttribute != model.DutyAttribute)
                    insertsys.Insert("职务子集", oldmodel.DutyAttribute, model.DutyAttribute,
                        model.EmpBasicID, FangShi, "职务属性");
                if (oldmodel.OrganizationName != model.OrganizationName)
                    insertsys.Insert("职务子集", oldmodel.OrganizationName, model.OrganizationName,
                        model.EmpBasicID, FangShi, "任职机构名称");

                if (oldmodel.Sec != model.Sec)
                    insertsys.Insert("职务子集", oldmodel.Sec, model.Sec,
                        model.EmpBasicID, FangShi, "集体内排序号");
                if (oldmodel.WorkCharge != model.WorkCharge)
                    insertsys.Insert("职务子集", oldmodel.WorkCharge, model.WorkCharge,
                        model.EmpBasicID, FangShi, "分管（从事）工作");
                if (oldmodel.ServiceMode != model.ServiceMode)
                    insertsys.Insert("职务子集", oldmodel.ServiceMode, model.ServiceMode,
                        model.EmpBasicID, FangShi, "任职方式");
                if (oldmodel.IsExceptionalPromotion != model.IsExceptionalPromotion)
                    insertsys.Insert("职务子集", oldmodel.IsExceptionalPromotion, model.IsExceptionalPromotion,
                        model.EmpBasicID, FangShi, "是否破格提拔");
                if (oldmodel.JobStatus != model.JobStatus)
                    insertsys.Insert("职务子集", oldmodel.JobStatus, model.JobStatus,
                        model.EmpBasicID, FangShi, "任职状态");
                if (oldmodel.JobDate != model.JobDate)
                    insertsys.Insert("职务子集", oldmodel.JobDate == null ? null : Convert.ToDateTime(oldmodel.JobDate).ToShortDateString(),
                        model.JobDate == null ? null : Convert.ToDateTime(model.JobDate).ToShortDateString(),
                        model.EmpBasicID, FangShi, "任现职务层次时间");
                if (oldmodel.RenQi != model.RenQi)
                    insertsys.Insert("职务子集", oldmodel.RenQi.ToString(), model.RenQi.ToString(),
                        model.EmpBasicID, FangShi, "任期");
                if (oldmodel.RZSYQ != model.RZSYQ)
                    insertsys.Insert("职务子集", oldmodel.RZSYQ.ToString(), model.RZSYQ.ToString(),
                        model.EmpBasicID, FangShi, "任职试用期");
                if (oldmodel.BRZDQRQ != model.BRZDQRQ)
                    insertsys.Insert("职务子集", oldmodel.BRZDQRQ == null ? null : Convert.ToDateTime(oldmodel.BRZDQRQ).ToShortDateString(),
                        model.BRZDQRQ == null ? null : Convert.ToDateTime(model.BRZDQRQ).ToShortDateString(),
                        model.EmpBasicID, FangShi, "本任职到期时间");
                if (oldmodel.RXRZWSXSJ != model.RXRZWSXSJ)
                    insertsys.Insert("职务子集", oldmodel.RXRZWSXSJ == null ? null : Convert.ToDateTime(oldmodel.RXRZWSXSJ).ToShortDateString(),
                        model.RXRZWSXSJ == null ? null : Convert.ToDateTime(model.RXRZWSXSJ).ToShortDateString(),
                        model.EmpBasicID, FangShi, "任现职务属性时间");
                if (oldmodel.LianRen != model.LianRen)
                    insertsys.Insert("职务子集", oldmodel.LianRen, model.LianRen,
                        model.EmpBasicID, FangShi, "是否连任");

                if (oldmodel.GRStartDate != model.GRStartDate)
                    insertsys.Insert("职务子集", oldmodel.GRStartDate == null ? null : Convert.ToDateTime(oldmodel.GRStartDate).ToShortDateString(),
                        model.GRStartDate == null ? null : Convert.ToDateTime(model.GRStartDate).ToShortDateString(),
                        model.EmpBasicID, FangShi, "挂任开始日期");
                if (oldmodel.GREndDate != model.GREndDate)
                    insertsys.Insert("职务子集", oldmodel.GREndDate == null ? null : Convert.ToDateTime(oldmodel.GREndDate).ToShortDateString(),
                        model.GREndDate == null ? null : Convert.ToDateTime(model.GREndDate).ToShortDateString(),
                        model.EmpBasicID, FangShi, "挂任结束日期");
            }

        }
    }
}
